import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;

import javax.sql.rowset.WebRowSet;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class StudentUI extends JFrame implements ActionListener{
	private JTextField txtMaSV;
	private JTextField txtTenSV;
	private JTextField txtLop;
	private JTextField txtKhoa;
	
	JButton btnNew,btnSave,btnReset,btnDelete,btnExit,btnUpdate;
	
	JTable table;
	DefaultTableModel model;
	
	WebRowSet rs=null;
	DataProcess data=null;
	
	public StudentUI() {
		getContentPane().setLayout(null);
		
		try {
			try {
				data=(DataProcess)Naming.lookup("//khangutc/dkh");
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
		
		JLabel lblDanhSachSinh = new JLabel("Danh Sach Sinh Vien");
		lblDanhSachSinh.setForeground(Color.RED);
		lblDanhSachSinh.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblDanhSachSinh.setBounds(177, 11, 144, 31);
		getContentPane().add(lblDanhSachSinh);
		
		JLabel lblMaSv = new JLabel("Ma SV");
		lblMaSv.setBounds(10, 64, 46, 21);
		getContentPane().add(lblMaSv);
		
		JLabel lblHoTen = new JLabel("Ho Ten");
		lblHoTen.setBounds(10, 109, 56, 21);
		getContentPane().add(lblHoTen);
		
		JLabel lblLop = new JLabel("Lop");
		lblLop.setBounds(251, 67, 46, 14);
		getContentPane().add(lblLop);
		
		JLabel lblKhoa = new JLabel("Khoa");
		lblKhoa.setBounds(251, 112, 46, 14);
		getContentPane().add(lblKhoa);
		
		txtMaSV = new JTextField();
		txtMaSV.setBounds(66, 64, 160, 20);
		getContentPane().add(txtMaSV);
		txtMaSV.setColumns(10);
		
		txtTenSV = new JTextField();
		txtTenSV.setBounds(66, 109, 160, 20);
		getContentPane().add(txtTenSV);
		txtTenSV.setColumns(10);
		
		txtLop = new JTextField();
		txtLop.setBounds(311, 64, 165, 20);
		getContentPane().add(txtLop);
		txtLop.setColumns(10);
		
		txtKhoa = new JTextField();
		txtKhoa.setBounds(311, 109, 165, 20);
		getContentPane().add(txtKhoa);
		txtKhoa.setColumns(10);
		
		/**
		 * Table Settings
		 */
		String[] header={"Ma SV","Ho Ten SV","Khoa","Ma Lop"};
		model= new DefaultTableModel(header,0);
		
		table=new JTable(model);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				String id=String.valueOf(table.getValueAt(table.getSelectedRow(), 0));
				String name=String.valueOf(table.getValueAt(table.getSelectedRow(), 1));
				String khoa=String.valueOf(table.getValueAt(table.getSelectedRow(), 2));
				String lop=String.valueOf(table.getValueAt(table.getSelectedRow(), 3));
				txtMaSV.setText(id);
				txtTenSV.setText(name);
				txtKhoa.setText(khoa);
				txtLop.setText(lop);
				btnDelete.setEnabled(true);
				btnUpdate.setEnabled(true);
				btnReset.setEnabled(true);
			}
		});
		fillTable();
		
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(10, 171, 478, 109);
		getContentPane().add(scrollPane);
		
		btnNew = new JButton("New");
		btnNew.setBounds(10, 291, 89, 23);
		getContentPane().add(btnNew);
		
		btnUpdate = new JButton("Update");
		btnUpdate.setBounds(109, 291, 89, 23);
		getContentPane().add(btnUpdate);
		
		btnDelete = new JButton("Delete");
		btnDelete.setBounds(208, 291, 89, 23);
		getContentPane().add(btnDelete);
		
		btnExit = new JButton("Exit");
		btnExit.setBounds(409, 291, 79, 23);
		getContentPane().add(btnExit);
		
		btnReset = new JButton("Reset");
		btnReset.setBounds(311, 291, 89, 23);
		getContentPane().add(btnReset);
		
		btnSave = new JButton("Save");
		btnSave.setBounds(10, 327, 89, 23);
		getContentPane().add(btnSave);
		
		JButton[] btn={btnNew,btnSave,btnReset,btnDelete,btnExit,btnUpdate};
		int i;
		for(i=0;i<btn.length;i++){
			btn[i].addActionListener(this);
		}
	}
	
	public static void main(String[] args){
		new StudentUI().setVisible(true);
	}
	
	protected void fillTable(){
		try {
			rs= data.GetTable("Student");
			while(rs.next()){
				String[] row={rs.getString(1),rs.getString(2),String.valueOf(rs.getInt(3)),rs.getString(4)};
				model.addRow(row);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	
	}
	
	/**
	 * Update table khi them moi thanh cong
	 */
	protected void updateRow(){
		String condition="StudentID='" + txtMaSV.getText() + "'";
			try {
				rs=data.GetRecord(condition,"Student");
				while(rs.next()){
					String[] row={String.valueOf(rs.getString(1)),rs.getString(2),String.valueOf(rs.getInt(3)),rs.getString(4)};
					model.addRow(row);			
				}
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		
	}
	
	/**
	 * Cap nhat lai cho table khi update thanh cong
	 */
	protected void updateValue() {
		String Ten=txtTenSV.getText();
		String Khoa=txtKhoa.getText();
		String Lop=txtLop.getText();
		
		table.setValueAt(Ten,table.getSelectedRow(),1);	
		table.setValueAt(Khoa,table.getSelectedRow(),2);	
		table.setValueAt(Lop,table.getSelectedRow(),3);	
	}
	
	/**
	 * Cap nhat lai cho table khi delete thanh cong
	 */
	protected void updateValuedelete() {
		model.removeRow(table.getSelectedRow());		
	}
	
	/**
	 * Xu ly them moi du lieu
	 */
	protected boolean insertData(String id, String ten, int Khoa, String Lop) {
		String sql="insert into Student values('" + id + "','" + ten + "'," + Khoa + ",'" + Lop + "')";
		try {
			data.ExecUpdateSQL(sql);
			return true;
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * Xu ly delete 1 ban ghi
	 * @param id
	 * @return
	 */
	protected boolean deleteData(String id){
		String sql="delete from Student where StudentID='" + id + "'";
		try {
			data.ExecUpdateSQL(sql);
			return true;
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * Xu ly update du lieu
	 */
	protected boolean updateDate(String id, String name, int Khoa, String Lop){
		String sql="update Student set StudentName='" + name.toString() + "',Course=" + Khoa + ",Location='" + Lop + " where StudentID='" + id + "'";
		try {
			data.ExecUpdateSQL(sql);
			return true;
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * Ham reset values
	 */
	protected void resetValues(){
		txtMaSV.setText("");
		txtTenSV.setText("");
		txtKhoa.setText("");
		txtLop.setText("");
		txtMaSV.setEnabled(false);
		btnNew.setEnabled(true);
		btnDelete.setEnabled(false);
		btnSave.setEnabled(false);
		btnReset.setEnabled(false);
		btnUpdate.setEnabled(false);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnNew){
			resetValues();
			txtMaSV.setEnabled(true);
			txtMaSV.requestFocus();
			btnSave.setEnabled(true);
			btnReset.setEnabled(true);
		}
		
		if(e.getSource()==btnSave){
			String id=txtMaSV.getText();
			String name=txtTenSV.getText();
			int khoa = Integer.parseInt(txtKhoa.getText());
			String lop = txtLop.getText();			
			if(insertData(id, name,khoa,lop)){
				JOptionPane.showMessageDialog(null,"Save successed!");
				updateRow();
			}else{
				JOptionPane.showMessageDialog(null,"Insert failed!");
			}
		}
		
		if(e.getSource()==btnReset){
			resetValues();
		}
		
		if(e.getSource()==btnDelete){
			String id=txtMaSV.getText();
			if(deleteData(id)){
				JOptionPane.showMessageDialog(null,"Deleted!");
				updateValuedelete();
			}
			else{
				JOptionPane.showMessageDialog(null,"Delete failed!");
			}
			resetValues();
		}
		
		if(e.getSource()==btnUpdate){
			String id=txtMaSV.getText();
			String name=txtTenSV.getText();
			int Khoa= Integer.parseInt(txtKhoa.getText());
			String lop= txtLop.getText();
			if(updateDate(id, name,Khoa,lop)){
				JOptionPane.showMessageDialog(null,"Updated!");
				updateValue();
			}
			else{
				JOptionPane.showMessageDialog(null,"Update failed!");
			}
			resetValues();
		}
		
		if(e.getSource()==btnExit){
			System.exit(0);
		}
		
		
	}
}
